package com.nsl.my_youlai.mystar;

import java.util.ArrayList;
import java.util.List;

public class NslUtilList {
    public static int repeatTimesOfObject(List list,Object object){
        int repeatTimes=0;
        if(list==null || list.size()==0){
            return 0;
        }else{
            for (Object o : list) {
                if(o.equals(object)){
                    repeatTimes++;
                }
            }
            return repeatTimes;
        }
    }

    /**
     * 讲 “1,3,4,11” 类型的字符串，转换成int List
     * @return
     */
//    public static List<Integer> string2IntList(String s){
//        String[] intStrings=s.split(",");
//        List<Integer> integerList=new ArrayList<>();
//        for (String intString : intStrings) {
//            integerList.add(Integer.parseInt(intString));
//        }
//        return integerList;
//    }

    /**
     * 将形如 [1,2,3]的list，变成“1-2-3”的字符串
     * @param list
     * @param separator
     * @return
     */
    public static String list2String(List list,String separator){
        String s="";
        if(list==null || list.size()==0){
            return "";
        }else{
            for (Object o : list) {
                s+=o.toString()+separator;
            }
        }
        return s.substring(0,s.length()-1);
    }
    public static String list2String(List list){
        return list2String(list,"_");
    }

    /**
     * 将形如"1_12_44" => [1,12,44] list
     * @param s
     * @return
     */
    static public List<Integer> string2IntList(String s){
       return string2IntList(s,",");
    }

    /**
     * 将形如"1_12_44" => [1,12,44] list
     * @param s
     * @param separator
     * @return
     */
    static public List<Integer> string2IntList(String s, String separator){
        String[] strings=s.split(separator);
        List<Integer> list=new ArrayList<>();
        for (String string : strings) {
            Integer i=Integer.parseInt(string);
            list.add(i);
        }
        return list;
    }

}
